package com.wheat.vipcard.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionSupport;
import com.wheat.vipcard.bean.Card;
import com.wheat.vipcard.bean.CardDAO;
import com.wheat.vipcard.bean.Zone;
import com.wheat.vipcard.utils.PageResultSet;
import com.wheat.vipcard.utils.PageUtil;

public class VipCardAction extends ActionSupport {

	private static final Log LOG = LogFactory.getLog(VipCardAction.class);

	/**
	 * ��Ա��������
	 */
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request = null;

	private Card card;

	// ���ؽ���ͻ���
	private String result;

	private Map<String, Object> dataMap;

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Card getCard() {
		return card;
	}

	public void setCard(Card card) {
		this.card = card;
	}

	public String saveVip() {
		System.out.println("card =" + card);
		return "saveVip";
	}

	/**
	 * ����̼�
	 * 
	 * @return
	 * @throws Exception
	 */
	public String savecard() throws Exception {
		Transaction tr = null;
		System.out.println("card =" + card);
		try {
			if (card != null) {
				CardDAO dao = new CardDAO();
				tr = dao.getSession().beginTransaction();
				dao.save(card);
				tr.commit();
				dao.getSession().close();
			}
		} catch (Exception e) {
			tr.rollback();
			e.printStackTrace();
		}
		return "savecard";
	}

	/**
	 * �������е��̼�
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getAllCard() throws Exception {
		try {
			CardDAO dao = new CardDAO();
			dataMap = new HashMap<String, Object>();
			dataMap.put("data", dao.findAll());
			dataMap.put("msg", "�ɹ�");
			dataMap.put("ret", 1);
			System.out.println("dataMap :" + dataMap);
			return SUCCESS;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * ����̼�ID��ȡ����
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getCardById() throws Exception {
		try {

			HttpServletRequest request = ServletActionContext.getRequest();
			String disId = request.getParameter("disId");
			System.out.println("disId =" + disId);
			CardDAO dao = new CardDAO();
			int diid = Integer.parseInt(disId);
			dataMap = new HashMap<String, Object>();
			dataMap.put("data", dao.findByBsId(diid));
			dataMap.put("msg", "�ɹ�");
			dataMap.put("ret", 1);
			System.out.println("result :" + result);
			return SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}

	/**
	 * ��ȡ��ͬ���Ļ�Ա��
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getCardByType() throws Exception {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String cardType = request.getParameter("cardType");
			System.out.println("cardType =" + cardType);
			CardDAO dao = new CardDAO();
			int cardT = Integer.parseInt(cardType);
			dataMap = new HashMap<String, Object>();
			dataMap.put("data", dao.findByCardType(cardT));
			dataMap.put("msg", "�ɹ�");
			dataMap.put("ret", 1);
			System.out.println("result :" + result);

			return SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}

	/**
	 * 分页查询
	 * 
	 * @return
	 */
	public String listCardByPage() {
		CardDAO cardDao = new CardDAO();
		request = ServletActionContext.getRequest();
		try {
			int sPage = Integer.valueOf((String) request.getParameter("page"));
			PageResultSet<Card> pageResultSetList = cardDao.findCardByPage(card, sPage, new PageUtil().getPageSize());
			Map<String, Object> resultJsonMap = new HashMap<String, Object>();
			resultJsonMap.put("event", pageResultSetList.getList());
			resultJsonMap.put("pageBean", pageResultSetList.getPage());
			resultJsonMap.put("pageCount", pageResultSetList.getPage().getTotalPage());
			this.result = JSONObject.fromObject(resultJsonMap).toString();
			return SUCCESS;
		} catch (Exception e) {
			if (LOG.isErrorEnabled()) {
				LOG.error("System error!!!" + e);
			}
		}
		return ERROR;
	}

}
